<template>
	<view>
		<u-sticky>
			<view class="sticky_box">
				<u-search placeholder="请输入SN号搜索" v-model="search" @custom="custom"></u-search>
			</view>
			<view class="sticky_screen">
				<view class="datetime">
					<u-datetime-picker style="flex: 0;" :show="start" v-model="start_value" mode="date"
						:min-date="minday" :max-date="maxday" @confirm="confirm($event,1)"
						@cancel="cancel(1)"></u-datetime-picker>
					<text class="datetime" @click="start = true">{{start_text}}</text>
					<view>至</view>
					<u-datetime-picker style="flex: 0;" :show="end" v-model="end_value" mode="date" :min-date="minday"
						:max-date="maxday" @confirm="confirm($event,2)" @cancel="cancel(2)">
					</u-datetime-picker>
					<text class="datetime" @click="end = true">{{end_text}}</text>
				</view>
				<view class="blue" @click="time(0)">昨天</view>
				<view class="blue" @click="time(1)">近7天</view>
				<view class="blue" @click="time(2)">本月</view>
			</view>
		</u-sticky>
		<view class="profit_list_box" v-if="display == true">
			<view class="profit_list" v-for="(item,index) in data.data" :key="index">
				<view class="profit_list_tit">
					<u-tag :text="item.mome" :type="tag_type" plain size="mini"></u-tag>
					<text>{{item.sn}}</text>
				</view>
				<view class="profit_list_cont">
					<view class="profit_list_cont_time">
						<text>{{item.merchant_name}}</text>
						<h5>+{{item.changes}}</h5>
					</view>
					<view class="profit_list_cont_time">
						<u-tag :text="item.product_name" size="mini" style="margin-top: 20rpx;"></u-tag>
						<u--text :text="item.time" color="#999999" align="right" size="26rpx"
							margin="20rpx 0 0 0 "></u--text>
					</view>
				</view>
			</view>
		</view>
		<!-- //暂无数据 -->
		<u-empty v-else mode="list" icon="/static/Empty/data_null.png">
		</u-empty>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				start: false,
				start_text: '开始日期',
				start_value: Number(new Date()),
				start_time: '',
				end: false,
				end_text: '结束日期',
				end_value: Number(new Date()),
				end_time: '',
				type_id: null,
				type: '',
				data: [],
				page: 1,
				bottom: true,
				display: null,
				search: '',
				tag_type: 'primary',
			}
		},
		onLoad(e) {
			this.type_id = e.type_id
			this.type = e.type
			this._request()
			uni.setNavigationBarTitle({
				title: e.name + ' - 详情'
			})
			this.switchTag(e.type_id)
		},
		//上拉加载
		onReachBottom() {
			if (this.bottom == true) {
				this.page += 1
				this._request()
			}
		},
		computed: {
			maxday() {
				let day = new Date();
				day.setTime(day.getTime() - 24 * 60 * 60 * 1000);
				let days = day.getFullYear() + "-" + (day.getMonth() + 1) + "-" + day.getDate()
				let max_day = new Date(days).getTime()
				return max_day
			},
			minday() {
				let day = new Date(); // 获取今天的日期
				day.setDate(day.getDate() - 180) // 今天的前N天的日期，N自定义
				let days = day.getFullYear() + '-' + (day.getMonth() + 1) + '-' + day.getDate()
				let min_day = new Date(days).getTime()
				return min_day
			}
		},
		methods: {
			time(type) {
				this.page = 1
				this.data = []
				switch (type) {
					case 0:
						//昨天的时间
						var day1 = new Date();
						day1.setTime(day1.getTime() - 24 * 60 * 60 * 1000);
						var yesterday = day1.getFullYear() + "-" + (day1.getMonth() + 1) + "-" + day1.getDate();
						this.start_time = yesterday
						this.end_time = yesterday
						this._request()
						break
					case 1:
						//一周
						const today = new Date();
						let lastWeek = today.getDate() - 7;
						let Week = new Date(today.setDate(lastWeek));
						let now = new Date().getDate() - 1;
						let _now = new Date(today.setDate(now));
						this.start_time = Week.getFullYear() + "-" + (Week.getMonth() + 1) + "-" + Week.getDate()
						this.end_time = _now.getFullYear() + "-" + (_now.getMonth() + 1) + "-" + _now.getDate()
						this._request()
						break
					case 2:
						//本月
						var date = new Date();
						let endDate = date.getDate() - 1;
						let _endDate = new Date(date.setDate(endDate));
						date.setDate(1);
						let beginDate = date.toISOString().slice(0, 10)
						this.start_time = beginDate
						this.end_time = _endDate.getFullYear() + "-" + (_endDate.getMonth() + 1) + "-" + _endDate.getDate()
						this._request()
						break
				}
			},
			custom() {
				this.page = 1
				this.data = []
				this._request()
			},
			confirm(e, id) {
				const timeFormat = uni.$u.timeFormat
				switch (id) {
					case 1:
						//开始的时间
						this.start_text = timeFormat(e.value, 'yyyy-mm-dd')
						this.start_time = timeFormat(e.value, 'yyyy-mm-dd')
						this.start = false
						break
					case 2:
						//结束的时间
						this.end_text = timeFormat(e.value, 'yyyy-mm-dd')
						this.end_time = timeFormat(e.value, 'yyyy-mm-dd')
						this.end = false
						break
				}
				if (this.start_time != '' && this.end_time != '') {
					this.page = 1
					this.data = []
					this._request()
				}
			},
			cancel(id) {
				if (id == 1) {
					this.start = false
				} else if (id == 2) {
					this.end = false
				}
			},
			async _request() {
				uni.showLoading({
					title: '加载中'
				});
				const userinfo = uni.getStorageSync('userinfo');
				const {
					data: res
				} = await this.$myHttp({
					url: 'balance/getMoneyDetails',
					data: {
						balance_type_id: this.type_id,
						money_type: this.type,
						page: this.page,
						page_size: 15,
						start_time: this.start_time,
						end_time: this.end_time,
						sn: this.search
					},
					header: {
						'token': userinfo.token
					},
				});
				if (res.code == 1) {
					if (res.data.data !== undefined && res.data.data !== null && res.data.data.length >
						0) {
						if (this.page == 1) {
							this.data = res.data
							this.display = true
							uni.hideLoading()
						} else {
							this.data.data = [...this.data.data, ...res.data.data]
							uni.hideLoading()
							this.display = true
						}
					} else {
						if (this.page == 1) {
							uni.hideLoading()
							this.bottom = false
							this.display = false
						} else {
							uni.hideLoading()
							this.bottom = false
							this.display = true
						}
					}
				} else {
					uni.showToast({
						title: res.msg,
						duration: 1000,
						icon: "none"
					});
					this.bottom = false
					this.display = false
				}
			},
			switchTag (type_id) {
				if (type_id == 1) {
					this.tag_type = 'primary'
				}
				if (type_id == 2) {
					this.tag_type = 'warning'
				}
				if (type_id == 3) {
					this.tag_type = 'success'
				}
				if (type_id == 4) {
					this.tag_type = 'error'
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.sticky_box {
		padding: 30rpx;
		background-color: #fff;
		box-sizing: border-box;
	}

	.sticky_screen {
		display: flex;
		align-items: center;
		justify-content: space-evenly;
		padding: 30rpx;
		padding-top: 0;
		background-color: #fff;

		.datetime {
			display: flex;
			align-items: center;
			width: 50%;
			background-color: #55aaff;
			border-radius: 100rpx;
			padding: 10rpx 20rpx;
			font-size: 24rpx;
			color: #fff;

			text {
				display: flex;
				justify-content: center;
				padding: 0;
			}
		}

		.blue {
			background-color: #55aaff;
			color: #fff;
			border-radius: 100rpx;
			padding: 10rpx 20rpx;
			font-size: 24rpx;
		}
	}

	.profit_list_box {
		padding: 50rpx;
		box-sizing: border-box;

		.profit_list {
			margin-bottom: 30rpx;
			border: #eee 1px solid;
			border-radius: 15rpx;

			.profit_list_tit {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 20rpx;
				box-sizing: border-box;

				.tit_name {
					border: #55aaff 1px solid;
					border-radius: 15rpx;
					padding: 10rpx 20rpx;
					color: #55aaff;
				}

				text {
					font-size: 28rpx;
				}
			}

			.profit_list_cont {
				padding: 30rpx 20rpx;
				padding-top: 0;
				box-sizing: border-box;

				h5 {
					font-size: 36rpx;
					// margin-bottom: 10rpx;
					color: #df3339;

					text {
						font-size: 28rpx;
						font-weight: 500;
						padding-left: 10rpx;
					}
				}

				text {
					font-size: 32rpx;
					color: #333;
				}

				.profit_list_cont_time {
					display: flex;
					align-items: center;
					justify-content: space-between;
				}
			}
		}
	}
</style>